package com.duoec.magichour.controller.api;

import com.duoec.magichour.core.exceptions.BusinessException;
import com.duoec.magichour.core.utils.MobileUtils;
import com.duoec.magichour.core.web.BaseApiController;
import com.duoec.magichour.dto.BaseResponse;
import com.duoec.magichour.service.SmsService;
import com.duoec.magichour.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * 短信接口
 */
@RestController
@RequestMapping("/api/sms")
public class SmsApiController extends BaseApiController {
    @Autowired
    private SmsService smsService;

    @Autowired
    private UserService userService;

    /**
     * 获取手机验证码
     *
     * @param mobile 手机号码
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/code/{mobile:\\d{11}}")
    public BaseResponse<String> sendMobileCode(
            @PathVariable String mobile
    ) {
        if (!MobileUtils.isAvailabeMobile(mobile)) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "无效手机号码！");
        }
        String mseeage = smsService.getCode(mobile);
        return BaseResponse.success(mseeage);
    }

    /**
     * 初始化手机登录密码
     *
     * @param mobile 手机号码
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/pwd/{mobile:\\d{11}}")
    public BaseResponse<String> setMobilePwd(
            @PathVariable String mobile
    ) {
        if (!MobileUtils.isAvailabeMobile(mobile)) {
            throw new BusinessException(BaseResponse.CODE_PARAM_INVALID, "无效手机号码！");
        }

        String mseeage = userService.resetMobilePwd(mobile);
        return BaseResponse.success(mseeage);
    }

}
